package com.hxzy.controller.system;

import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.TreeSelect;
import com.hxzy.common.core.domain.entity.SysMenu;
import com.hxzy.system.domain.dto.SysMenuSearchDTO;
import com.hxzy.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api")
@Api(tags = "菜单API")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService sysMenuService;

    @ApiOperation(value = "根据条件查询菜单")
    @GetMapping(value = "/system/menu/list")
    public R search(SysMenuSearchDTO sysMenuSearchDTO){
        List<SysMenu> sysMenuList = this.sysMenuService.selectMenuList(sysMenuSearchDTO);
        return R.okHasData(sysMenuList);
    }


//    @ApiOperation(value = "加载树型菜单数据")
//    @GetMapping(value = "/system/menu/treeselect")
//    public R menuTreeSelect(){
//        //根据条件查询菜单，这里没有给对象赋值，相当于全查询
//        List<SysMenu> menus = sysMenuService.selectMenuList(new SysMenuSearchDTO());
//        //把SysMenu转换成树型节点
//        List<TreeSelect> treeSelects=this.sysMenuService.buildMenuTreeSelect(menus);
//        return R.okHasData(treeSelects);
//    }

    @ApiOperation(value = "加载对应角色菜单列表树")
    @GetMapping(value = "/system/menu/roleMenuTreeselect/{roleId}")
    public R roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        //根据条件查询菜单，这里没有给对象赋值，相当于全查询
        List<SysMenu> menus = sysMenuService.selectMenuList(new SysMenuSearchDTO());

        //根据该角色id拥有的菜单
        List<Long> checkMenu=  this.sysMenuService.selectMenuListByRoleId(roleId);

        //把SysMenu转换成树型节点
        List<TreeSelect> treeSelects=this.sysMenuService.buildMenuTreeSelect(menus);

        //组装数据到前端
        Map<String,Object> data=new HashMap<>();
        data.put("checkedKeys", checkMenu);
        data.put("menus", treeSelects);

        return R.okHasData(data);
    }
}
